import {template} from "./template";
import {formatDate} from "./formatDate";
import {startsWith} from "./startsWith";

export enum Level {
  ROOT = 5,
  DEBUG = 4,
  ERROR = 3,
  WARN = 2,
  INFO = 1
}

const _Level = {
  5: {level: "info", color: "color: #008ed2;"},
  4: {level: "debug", color: "color: black;"},
  3: {level: "error", color: "color:red;"},
  2: {level: "warn", color: "color: yellow;"},
  1: {level: "info", color: "color: #008ed2;"}
}

let configure = {
  appenderCategoryPrefixs: ["*"],
  level: Level.ROOT
};

function configureLogger(appenderCategoryPrefixs: Array<string>, level: Level) {
  this.configure.appenderCategoryPrefixs = appenderCategoryPrefixs;
  this.configure.level = level;
}

let slice = [].slice;

class Logger {
  constructor(private category: string) {
  }

  private print(level: Level, args): void {
    if (configure.level >= level && configure.appenderCategoryPrefixs.find(prefix => prefix == "*" ? true : startsWith(prefix, this.category))) {
      let message = [
        "%c%s", formatDate("[yyyy-MM-dd HH:mm:ss]", new Date()) + " [" + _Level[level].level.toUpperCase() + "] " + this.category
      ].concat(args);
      console.info.apply({}, message)

      // console.log(message)
    }
  }

  info(...args): void {
    this.print(Level.INFO, slice.call(arguments, 0));
  }

  debug(...args): void {
    this.print(Level.DEBUG, slice.call(arguments, 0));
  }

  error(...args): void {
    this.print(Level.ERROR, slice.call(arguments, 0));
  }

  warn(...args): void {
    this.print(Level.WARN, slice.call(arguments, 0));
  }
}

function getLogger(category): Logger {
  return new Logger(category);
}

export {getLogger, configureLogger}
